import Vue from 'vue'
import Router, { RouteConfig } from 'vue-router'
import { RawLocation } from 'vue-router/types/router'
import Home from 'views/myApplication/index.vue'
// import Home from 'views/Home.vue'
import articleRoutes from './article'
import { AppModule } from '@/store/modules/app'
const originalPush = Router.prototype.push

Router.prototype.push = function push (location: RawLocation, onResolve?: Function, onReject?: any) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return (originalPush.call(this, location) as any).catch((err: Error) => err)
}
Vue.use(Router)
export let constantRoutes: RouteConfig[] = [
  {
    path: '/redirect',
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path*',
        component: () => import(/* webpackChunkName: "redirect" */ '@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/',
    name: 'home',
    component: Home,
    meta: {
      title: '首页',
      keepAlive: true
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import(/* webpackChunkName: "404" */ 'views/404.vue'),
    meta: {
      title: '404',
      keepAlive: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "article" */ '@/views/login/index.vue'),
    meta: {
      auth: true,
      title: 'login'
      // keepAlive: true
    }
  },
  {
    path: '/article',
    name: 'article',
    component: () => import(/* webpackChunkName: "article" */ 'views/Article.vue'),
    meta: {
      auth: true,
      title: '文章',
      keepAlive: true
    }
  },
  {
    path: '/detailApplication/:id',
    name: 'detailApplication',
    component: () => import(/* webpackChunkName: "article" */ 'views/myApplication/detail.vue'),
    meta: {
      auth: true,
      title: 'detailApplication',
      keepAlive: true
    }
  }
]
/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes: RouteConfig[] = [
  articleRoutes,
  {
    path: '*',
    redirect: '/404',
    meta: { hidden: true }
  }
]
const createRouter = () => new Router({
  // mode: 'history',  // Disabled due to Github Pages doesn't support this, enable this if you need.
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  base: process.env.BASE_URL,
  routes: constantRoutes
})
const myRouter = createRouter()
const history:any = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

myRouter.beforeEach((to:any, from:any, next:any) => {
// myRouter.beforeEach((to, from, next) => {
  if (to.params.direction) {
    AppModule.SetDirection(to.params.direction)
  } else {
    const toIndex = history.getItem(to.path)
    const fromIndex = history.getItem(from.path)
    // 判断并记录跳转页面是否访问过，以此判断跳转过渡方式
    if (toIndex) {
      if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
        AppModule.SetDirection('forward')
      } else {
        AppModule.SetDirection('back')
      }
    } else {
      ++historyCount
      history.setItem('count', historyCount)
      to.path !== '/' && history.setItem(to.path, historyCount)
      AppModule.SetDirection('forward')
    }
  }
  next()
})

export function resetRouter () {
  const newRouter = createRouter();
  (myRouter as any).matcher = (newRouter as any).matcher // reset router
}

export default myRouter
